Spring和MyBatis整合
MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中。它将允许 MyBatis 参与到 Spring 的事务管理之中,创建映射器 mapper 和 SqlSession
并注入到 bean 中,以及将 Mybatis 的异常转换为 Spring 的 DataAccessException
。 最终,可以做到应用代码不依赖于 MyBatis,Spring 或 MyBatis-Spring。
1. 所需依赖
- Junit
- MyBatis
- MySQL
- Spring-core
- Spring-context
- Spring-beans
- Spring-aop
- aspectjweaver
- MyBatis-Spring【new】
- Spring-JDBC【new】
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.9</version>
</dependency>
<!-- https://mvnrep
ository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.8.M1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.16.RELEASE</version>
</dependency>
</dependencies>
2. 配置MyBatis环境
3. 整合MyBatis
3.1. 方式一
此方式主要是将MyBatis中的配置迁移到Spring配置文件中,需要配置Mapper,同时需要多一个Mapper接口的实现类。
本案例在上述环境中做出更改
配置Spring的xml配置文件
将数据源导入到Spring中(相当于把db.properties导入)
在配置文件中配置SqlSessionFactory
配置SqlSession
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!--configuration核心配置文件--> <configuration> <typeAliases> <typeAlias type="org.gs.pojo.User" alias="user"/> </typeAliases> <mappers> <mapper resource="org/gs/mapper/UserMapper.xml" /> </mappers> </configuration>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--从db.properties获取db配置信息--> <context:property-placeholder location="db.properties"/> <!--将db配置信息配置到spring的数据源中--> <!-- 这里也可以选择其他数据源,如 c3p0 dbcp druid--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </bean> <!--配置SqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--将数据源内容注入--> <property name="dataSource" ref="dataSource"/> <!--可以携带mybatis原有的配置文件,一般用来指定别名--> <property name="configLocation" value="classpath:mybatis-config.xml" /> <!--此处也可以映射文件--> <!--其实mybatis配置文件中的配置都可以在这里完成--> <property name="mapperLocations" value="classpath:org/gs/mapper/UserMapper.xml"/> </bean> <!--配置SqlSession--> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <!--将SqlSessionFactory通过构造器注入,实际上也只有这一种注入方法--> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <!--这里需要多一个实现类,Spring需要进行依赖注入--> <!--配置mapper实现类,注入SqlSession--> <bean id="userMapper" class="org.gs.mapper.UserMapperImpl"> <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/> </bean> </beans>
UserMapper接口实现类
package org.gs.mapper; import org.gs.pojo.User; import org.mybatis.spring.SqlSessionTemplate; import java.util.List; /** * @author admin * @date 2021/9/13 7:23 下午 */ public class UserMapperImpl implements UserMapper{ // Spring管理的MyBatis需要注入,需要有一个具体实现 private SqlSessionTemplate sqlSessionTemplate; // 默认setter注入 public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSessionTemplate = sqlSessionTemplate; } @Override public List<User> getUsers() { UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class); return mapper.getUsers(); } }
测试方法
@Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = context.getBean("userMapper", UserMapper.class); userMapper.getUsers().forEach(System.out::println); }
3.2. 方式二
接口实现类继承SqlSessionDaoSupport类,可以不用在xml配置文件中配置SqlSessionTemplate
配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!--configuration核心配置文件--> <configuration> <typeAliases> <typeAlias type="org.gs.pojo.User" alias="user"/> </typeAliases> <mappers> <mapper resource="org/gs/mapper/UserMapper.xml" /> </mappers> </configuration>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="db.properties"/> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <!--配置mapper实现类,注入SqlSessionFactory--> <bean id="userMapper" class="org.gs.mapper.UserMapperImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> </beans>
实现类,继承SqlSessionDaoSupport类
package org.gs.mapper; import org.gs.pojo.User; import org.mybatis.spring.support.SqlSessionDaoSupport; import java.util.List; /** * @author admin * @date 2021/9/13 7:23 下午 */ public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper{ @Override public List<User> getUsers() { return getSqlSession().getMapper(UserMapper.class).getUsers(); } }
测试类
import org.gs.mapper.UserMapper; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author admin * @date 2021/9/13 6:10 下午 */ public class UserDaoTest { @Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = context.getBean("userMapper", UserMapper.class); userMapper.getUsers().forEach(System.out::println); } }
3.3. 方式三
动态实现Dao接口,无需编写Dao接口实现类
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="db.properties"/> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <!--配置mapper接口扫描包,动态的实现mapper接口可以注入到Spring容器中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--注入SqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <!--要扫描的dao包--> <property name="basePackage" value="org.gs.mapper" /> </bean> </beans>